package cn.eng.pet.service.impl;
import cn.eng.org.domain.Department;
import cn.eng.pet.domain.PetType;
import cn.eng.pet.mapper.PetTypeMapper;
import cn.eng.pet.service.IPetTypeService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> petTypeTree() {
        List<PetType> petTypes=petTypeMapper.loadAll();
        Map<Long,PetType> map = new HashMap<>();
        for (PetType petType: petTypes){
            map.put(petType.getId(),petType);
        }
        List<PetType> petTypeTree=new ArrayList<>();
        for (PetType petType: petTypes){
            if (petType.getParentId()==null){
                petTypeTree.add(petType);
            }else {
                Long petTypeId = petType.getParentId();
                PetType parentPetType =  map.get(petTypeId);
                if (parentPetType!=null){
                    parentPetType.getChildren().add(petType);
                }
            }
        }
        return petTypeTree;
    }
}
